package com.tool.role;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.tool.time.TimeLimit;
import org.come.bean.LoginResult;
import org.come.bean.PrivateData;
import org.come.bean.TeamBean;
import org.come.entity.*;
import org.come.model.Title;
import org.come.until.AnalysisString;
import org.come.until.GoodsListFromServerUntil;
import org.come.until.SendRoleAndRolesummingUntil;
import org.come.until.UserMessUntil;
import org.come.bean.RoleAttribute;

import com.tool.image.ImageMixDeal;

public class RoleData {
	private static RoleData roleData;
	//玩家数据
	private LoginResult loginResult;
	// 背包记录数据
	private PackRecord packRecord;
	// 系统设置
	private RoleSystem roleSystem;
	// 私密数据
	private PrivateData privateData;
	// 召唤兽支援列表
	private List<BigDecimal> helpBb;
	// 多属性数据
	private RoleAttribute roleAttribute;
	private List fly = new ArrayList();//飞行器
	// 伙伴数据
	private List<Pal> pals;
	private List<Hang> helpFs;
	private TeamBean teamBean;
	// 召唤兽排序列表
	private List<BigDecimal> petOrder;
	public static RoleData getRoleData() {
		if (roleData == null) {
			roleData = new RoleData();
		}
		return roleData;
	}

	public RoleData() {
		// TODO Auto-generated constructor stub
		this.packRecord = new PackRecord();
		this.roleSystem = new RoleSystem();
		this.privateData = new PrivateData();
		petOrder = new ArrayList<>();
		helpBb = new ArrayList<>();
	}
	public void init(PackRecord packRecord, RoleSystem roleSystem, PrivateData privateData, LoginResult role) {
		if (role!=null) {
			this.loginResult=role;
		}
		if (packRecord != null) {
			this.packRecord = packRecord;
			String bbs = packRecord.getHelpBb();
			if (bbs != null) {
				String[] v = bbs.split("\\|");
				helpBb.clear();
				for (int i = 0; i < v.length; i++) {
					BigDecimal a = new BigDecimal(v[i]);
					helpBb.add(a);
				}
			}


			String petIds = packRecord.getPetOrder();
			if (petIds != null) {
				String[] vs = petIds.split("\\|");
				petOrder.clear();
				for (int i = 0; i < vs.length; i++) {
					BigDecimal v = new BigDecimal(vs[i]);
					petOrder.add(v);
				}
			}
			RoleTX roleTX = RoleTX.getRoleTX();
			roleTX.Toggle2(0);
			roleTX.EToggle(-1);
			roleTX.EToggle(-2);
			roleTX.EToggle(-3);
			roleTX.EToggle(-4);
			List<String> list = this.packRecord.getPutTX();
			if (list != null) {
				for (int i = 0; i < list.size(); i++) {
					roleTX.EToggle(Integer.parseInt(list.get(i)));
				}
			}
			roleTX.chushihuaWing();
		}
		if (roleSystem != null) {
			this.roleSystem = roleSystem;
		}
		if (privateData != null) {
			this.privateData = privateData;
			String[] vs = privateData.getSkill("S");
			if (vs != null) {
				boolean is = false;
				List<Integer> list = new ArrayList<>();
				for (int i = 0; i < vs.length; i++) {
					String[] vss = vs[i].split("_");
					int id = Integer.parseInt(vss[0]);
					if (!list.contains(id)) {
						list.add(Integer.parseInt(vss[0]));
					} else {
						is = true;
					}
				}
				String[] ses = SkillUtil.getSepciesS(SkillUtil.getSepciesN(ImageMixDeal.userimg.getRoleShow().getSpecies_id()));
				for (int i = list.size() - 1; i >= 0; i--) {
					int id = SkillUtil.changeSkillId(list.get(i), ses);
					if (id != list.get(i)) {
						list.remove(i);
						is = true;
					}
				}
				if (is) {
					int sld = AnalysisString.shuliandu(ImageMixDeal.userimg.getRoleShow().getGrade());
					StringBuffer buffer = new StringBuffer();
					for (int i = list.size() - 1; i >= 0; i--) {
						if (buffer.length() != 0) {
							buffer.append("#");
						}
						buffer.append(list.get(i));
						buffer.append("_");
						buffer.append(sld);
					}
					this.privateData.setSkills("S", buffer.length() == 0 ? null : buffer.toString());
					SendRoleAndRolesummingUntil.sendRole(this.privateData);
				}
			}
		}
	}

	public void changeSkin(boolean is) {
		String skin = this.loginResult.getSkin();
		String skin2 = this.getSkin();
		if (skin == null) {
			skin = "";
		}

		if (skin2 == null) {
			skin2 = "";
		}

		if (!skin.equals(skin2)) {
			skin2 = "".equals(skin2) ? null : skin2;
			this.loginResult.setSkin(skin2);
			ImageMixDeal.userimg.getRoleShow().setSkin(this.loginResult.getSkin());
			ImageMixDeal.userimg.getRoleShow().setFlyskin(this.loginResult.getFlyskin());
			ImageMixDeal.userimg.changeskin(null);
			if (is) {
				List list = this.packRecord.getPutTX();
				if (list != null && list.size() != 0) {
					StringBuffer buffer = new StringBuffer();
					buffer.append("E");
					buffer.append((String)list.get(0));

					for(int i = 1; i < list.size(); ++i) {
						buffer.append("_");
						buffer.append((String)list.get(i));
					}

					if (skin2 == null) {
						skin2 = buffer.toString();
					} else {
						skin2 = skin2 + "|" + buffer;
					}
				}
			}

			GoodsListFromServerUntil.sendPackRecord(5, skin2);
		}

	}

	public String getSkin() {
		String fSkin = null;
		String eSkin = TimeLimit.getLimits().getSkin();
		if (eSkin == null) {
			Goodstable good = GoodsListFromServerUntil.getChoseGoodsList()[0];
			if (good != null) {
				long weaponSkin = good(Integer.parseInt(good.getSkin()));
				if (weaponSkin != 0L) {
					if (RoleProperty.getRoleProperty().getQhv() >= 12) {
						weaponSkin += 18L;
					}

					eSkin = String.valueOf(weaponSkin << 32 | this.loginResult.getSpecies_id().longValue());
				}
			}
		}

		Fly fly = this.loginResult.getFly_id() != null ? this.getFlyOne(this.loginResult.getFly_id()) : null;
		if (fly != null) {
			//fSkin = fly.getSkin();
		}

		StringBuffer buffer = this.packRecord.getPutTXBuffer();
		if (eSkin != null && !"".equals(eSkin)) {
			if (buffer == null) {
				buffer = new StringBuffer();
			} else if (buffer.length() != 0) {
				buffer.append("|");
			}

			buffer.append("S");
			buffer.append(eSkin);
		}

		if (fSkin != null && !"".equals(fSkin)) {
			if (buffer == null) {
				buffer = new StringBuffer();
			} else if (buffer.length() != 0) {
				buffer.append("|");
			}

			buffer.append("F");
			buffer.append(fSkin);
		}

		if (this.loginResult.getTitle() != null && !"".equals(this.loginResult.getTitle())) {
			Title title = UserMessUntil.getTitle(this.loginResult.getTitle());
			if (title != null && title.getSkin() != null && !"".equals(title.getSkin())) {
				if (buffer == null) {
					buffer = new StringBuffer();
				} else if (buffer.length() != 0) {
					buffer.append("|");
				}

				buffer.append("C");
				buffer.append(title.getSkin());
			}
		}

		if (GoodsListFromServerUntil.getChoseGoodsList()[12] != null) {
			if (buffer == null) {
				buffer = new StringBuffer();
			} else if (buffer.length() != 0) {
				buffer.append("|");
			}

			buffer.append("B");
			buffer.append(GoodsListFromServerUntil.getChoseGoodsList()[12].getSkin());
		}

		return buffer != null && buffer.length() != 0 ? buffer.toString() : null;
	}


	/**初始化列表*/
	public void addHelpBb(List<BigDecimal> Bbs) {
		for (int i = helpBb.size() - 1; i >= 0; i--) {
			if (!Bbs.contains(helpBb.get(i))) {
				helpBb.remove(i);
			}
		}
		for (int i = 0; i < Bbs.size(); i++) {
			if (!helpBb.contains(Bbs.get(i))) {
				helpBb.add(Bbs.get(i));
			}
		}
	}


	/**
	 * 根据列表获取召唤兽名称
	 */
	public List<String> getDepositBbName() {
		List<String> list = new ArrayList<>();
		List<RoleSummoning> pets = UserMessUntil.getDepositPetListTable();
		for (int i = 0; i < pets.size(); i++) {
			list.add(pets.get(i).getSummoningname());
		}
		return list;
	}

	/** 获取拥护闪现召唤兽列表 */
	public List<BigDecimal> getHelpBbId() {
		int[] vs = new int[] { 1806, 1820, 1821, 1822, 1823, 1824, 1825, 1826, 1827 };
		List<BigDecimal> list = new ArrayList<>();
		List<RoleSummoning> pets = UserMessUntil.getPetListTable();
		ss: for (int i = pets.size() - 1; i >= 0; i--) {
			String skill = pets.get(i).getPetSkills();
			if (skill == null || "".equals(skill)) {
				continue ss;
			}
			String[] v = skill.split("\\|");
			for (int j = 0; j < v.length; j++) {
				int id = Integer.parseInt(v[j]);
				for (int k = 0; k < vs.length; k++) {
					if (vs[k] == id) {
						list.add(pets.get(i).getSid());
						continue ss;
					}
				}
			}
		}
		return list;
	}
	public static int good(int id) {
		// 添加对双环武器的识别 (包括1100-1115范围以及特殊ID)
		if ((id >= 1100 && id <= 1115) || id == 6124 || id == 7021 || (id >= 2500 && id <= 2515)|| (id >= 2300 && id <= 2315)){
			return 11; // 双环类型
		}

		// 添加对灯笼武器的识别 (包括1700-1715范围以及特殊ID)
		if ((id >= 1700 && id <= 1715) || id == 6102 || id == 7008 ||  (id >= 2600 && id <= 2615)|| (id >= 2300 && id <= 2315)) {
			return 17; // 灯笼类型
		}
		if ((id < 1600 || id > 1615) && id != 6100 && id != 7006) {
			if ((id < 1400 || id > 1415) && id != 6106 && id != 7012) {
				if ((id < 1100 || id > 1115) && id != 6124 && id != 7021) {
					if ((id < 1200 || id > 1215) && id != 6122 && id != 7022) {
						if ((id < 2200 || id > 2215) && id != 6109 && id != 7016) {
							if ((id < 2400 || id > 2415) && id != 6119 && id != 7020) {
								if ((id < 1300 || id > 1315) && id != 6103 && id != 7009) {
									if ((id < 1700 || id > 1715) && id != 6102 && id != 7008) {
										if ((id < 2100 || id > 2115) && id != 7013 && id != 6105) {
											if ((id < 1000 || id > 1015) && id != 6118 && id != 7007) {
												if (id != 7019 && id != 6120) {
													if ((id < 1800 || id > 1815) && id != 6104 && id != 7011) {
														if ((id < 1900 || id > 1915) && id != 6108 && id != 7017) {
															if ((id < 2200 || id > 2215) && id != 6109 && id != 7010) {
																if ((id < 1500 || id > 1515) && id != 7014 && id != 6117) {
																	if ((id < 2000 || id > 2015) && id != 6107 && id != 7015) {
																		if (id != 7018 && id != 6121) {
																			return (id < 2617 || id > 2632) && id != 6125 && id != 7023 ? 0 : 18;
																		} else {
																			return 17;
																		}
																	} else {
																		return 16;
																	}
																} else {
																	return 15;
																}
															} else {
																return 14;
															}
														} else {
															return 13;
														}
													} else {
														return 12;
													}
												} else {
													return 11;
												}
											} else {
												return 10;
											}
										} else {
											return 9;
										}
									} else {
										return 8;
									}
								} else {
									return 7;
								}
							} else {
								return 6;
							}
						} else {
							return 5;
						}
					} else {
						return 4;
					}
				} else {
					return 3;
				}
			} else {
				return 2;
			}
		} else {
			return 1;
		}
	}

	/** 根据列表获取召唤兽名称 */
	public List<String> getHelpBbName(List<BigDecimal> Bbs) {
		List<String> list = new ArrayList<>();
		List<RoleSummoning> pets = UserMessUntil.getPetListTable();
		for (int i = 0; i < helpBb.size(); i++) {
			BigDecimal id = helpBb.get(i);
			for (int j = pets.size() - 1; j >= 0; j--) {
				if (pets.get(j).getSid().compareTo(id) == 0) {
					list.add(pets.get(j).getSummoningname());
					break;
				}
			}
		}
		return list;
	}

	// 切换位置返回变化后的名称 当前位置 变化后位置
	public List<String> CHelpBb(int v, int v2) {
		if (v2 > helpBb.size() - 1) {
			v2 = helpBb.size() - 1;
		}
		if (v > helpBb.size() - 1) {
			v = helpBb.size() - 1;
		}
		if (v == v2) {
			return null;
		}
		if (v < 0 || v2 < 0) {
			return null;
		}
		BigDecimal id = helpBb.remove(v);
		helpBb.add(v2, id);
		sendHelpBb();
		return getHelpBbName(helpBb);
	}

	// 存修改的召唤兽支援列表
	public void sendHelpBb() {
		StringBuffer buffer = new StringBuffer();
		for (int i = 0; i < helpBb.size(); i++) {
			if (i != 0) {
				buffer.append("|");
			}
			buffer.append(helpBb.get(i));
		}
		packRecord.setHelpBb(buffer.toString());
		GoodsListFromServerUntil.sendPackRecord(1, packRecord.getHelpBb());
	}

	/** 添加特效 */
	public void addTx(String id) {
		packRecord.addTX(id);
		RoleTX.getRoleTX().Toggle2(RoleTX.getRoleTX().getTxYs());
	}

	public PackRecord getPackRecord() {
		return packRecord;
	}

	public void setPackRecord(PackRecord packRecord) {
		this.packRecord = packRecord;
	}

	public RoleSystem getRoleSystem() {
		return roleSystem;
	}

	public void setRoleSystem(RoleSystem roleSystem) {
		this.roleSystem = roleSystem;
	}

	public List<BigDecimal> getHelpBb() {
		return helpBb;
	}

	public void setHelpBb(List<BigDecimal> helpBb) {
		this.helpBb = helpBb;
	}

	public PrivateData getPrivateData() {
		return privateData;
	}

	public void setPrivateData(PrivateData privateData) {
		this.privateData = privateData;
	}

	public LoginResult getLoginResult() {
		return loginResult;
	}

	public void setLoginResult(LoginResult loginResult) {
		this.loginResult = loginResult;
	}
	public List<Pal> getPals() {
		return pals;
	}
	public void setPals(List<Pal> pals) {
		this.pals = pals;
	}
	public Pal getPal(int pid){
		for (int i = pals.size()-1; i>=0; i--) {
			if (pals.get(i).getpId()==pid) {
				return pals.get(i);
			}
		}
		return null;
	}
	public void addPal(Pal pal){
		for (int i = pals.size()-1; i>=0; i--) {
			if (pals.get(i).getId().compareTo(pal.getId())==0) {
				pals.set(i, pal);
				return;
			}
		}
		pals.add(pal);
	}
	public TeamBean getTeamBean() {
		return teamBean;
	}
	public void setTeamBean(TeamBean teamBean) {
		this.teamBean = teamBean;
	}

	public List getFly() {
		return this.fly;
	}
	// 飞行器相关
	public Fly getFlyOne(Integer fid) {
		if (this.fly != null) {
			for(int i = 0; i < this.fly.size(); ++i) {
				Fly fly2 = (Fly)this.fly.get(i);
				if (fly2.getFlytid().compareTo(fid) == 0) {
					return fly2;
				}
			}
		}
		return null;
	}

	public void setFly(List fly) {
		this.fly = fly;
	}

	public RoleAttribute getRoleAttribute() {
		return roleAttribute;
	}
	public void setRoleAttribute(RoleAttribute roleAttribute) {
		this.roleAttribute = roleAttribute;
	}
	public List<String> PetBb(int v, int v2) {
		final List<RoleSummoning> list = new ArrayList<RoleSummoning>() {
			{
				this.addAll(UserMessUntil.getPetListTable());
			}
		};
		if (v2 > list.size() - 1) {
			v2 = list.size() - 1;
		}
		if (v > list.size() - 1) {
			v = list.size() - 1;
		}
		if (v == v2) {
			return null;
		}
		else {
			if (v < 0 || v2 < 0) {
				return null;
			}
			else {
				final RoleSummoning id = (RoleSummoning)list.remove(v);
				list.add(v2, id);
				this.sendPetBb(list);
				return new ArrayList<String>() {
					{
						list.forEach(pet -> this.add(pet.getSummoningname()));
					}
				};
			}
		}
	}

	public void sendPetBb(final List<RoleSummoning> list) {
		UserMessUntil.setPetListTable(list);
		final StringBuffer buffer = new StringBuffer();
		int i = 0;
		while (i < list.size()) {
			if (i != 0) {
				buffer.append("|");
			}
			buffer.append(((RoleSummoning)list.get(i)).getSid().toString());
			++i;
		}
		GoodsListFromServerUntil.sendPackRecord(6, buffer.toString());
	}
	public void sendHelpLingbao(String helpLingbao) {
		packRecord.setHelpLing(helpLingbao);
		GoodsListFromServerUntil.sendPackRecord(2, packRecord.getHelpLing());
	}
	public List<Hang> getHelpFs() {
		return helpFs;
	}

	public void setHelpFs(List<Hang> helpFs) {
		this.helpFs = helpFs;
	}
	public List<BigDecimal> getPetOrder() {
		return petOrder;
	}

	public void setPetOrder(List<BigDecimal> petOrder) {
		this.petOrder = petOrder;
	}
	// 切换位置返回变化后的名称 当前位置 变化后位置
	public List<String> updateOrderPet(int v, int v2) {
		if (v2 > petOrder.size() - 1) {
			v2 = petOrder.size() - 1;
		}
		if (v > petOrder.size() - 1) {
			v = petOrder.size() - 1;
		}
		if (v == v2) {
			return null;
		}
		if (v < 0 || v2 < 0) {
			return null;
		}
		BigDecimal id = petOrder.remove(v);
		petOrder.add(v2, id);
		sendOrderPet();
		return getOrderPetName();
	}

	// 存修改的召唤兽支援列表
	public void sendOrderPet() {
		StringBuffer buffer = new StringBuffer();
		for (int i = 0; i < petOrder.size(); i++) {
			if (i != 0) {
				buffer.append("|");
			}
			buffer.append(petOrder.get(i));
		}
		packRecord.setPetOrder(buffer.toString());
		GoodsListFromServerUntil.sendPackRecord(6, packRecord.getPetOrder());
	}

	public List<String> getOrderPetName() {
		List<String> list = new ArrayList<>();
		List<RoleSummoning> pets = UserMessUntil.getPetListTable();
		for (int i = 0; i < petOrder.size(); i++) {
			BigDecimal id = petOrder.get(i);
			for (int j = pets.size() - 1; j >= 0; j--) {
				if (pets.get(j).getSid().compareTo(id) == 0) {
					list.add(pets.get(j).getSummoningname());
					break;
				}
			}
		}
		return list;
	}

	public void addOrderPet(List<RoleSummoning> pets) {
		boolean is = false;
		List<BigDecimal> ids = new ArrayList<>();
		for (int i = 0; i < pets.size(); i++) {
			if (!petOrder.contains(pets.get(i).getSid())) {
				petOrder.add(pets.get(i).getSid());
				is = true;
			}
			ids.add(pets.get(i).getSid());
		}
		for (int i = petOrder.size() - 1; i >= 0; i--) {
			if (!ids.contains(petOrder.get(i))) {
				petOrder.remove(i);
				is = true;
			}
		}
		if (is) {
			sendOrderPet();
		}
	}

}
